package bma.common.langutil.jmshell.command;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import bma.common.langutil.jmshell.JMShellProcessor;
import bma.common.langutil.jmshell.JMShellUtil;
import bma.common.langutil.jmshell.Session;

public abstract class CommandProcessor implements JMShellProcessor {

	protected String command;
	protected String[] alias;
	protected String argsTips;

	public CommandProcessor(String command) {
		super();
		this.command = command;
	}

	public String getArgsTips() {
		return argsTips;
	}

	public void setArgsTips(String argsTips) {
		this.argsTips = argsTips;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public List<String> getAliasList() {
		if (alias == null)
			return new ArrayList<String>(0);
		return Arrays.asList(alias);
	}

	public void setAliasList(List<String> alias) {
		if (alias != null) {
			this.alias = alias.toArray(new String[0]);
		}
	}

	public void addAlias(String name) {
		List<String> list;
		if (this.alias == null) {
			list = new ArrayList<String>(1);
		} else {
			list = Arrays.asList(this.alias);
		}
		setAliasList(list);
	}

	public void setAlias(String name) {
		this.alias = new String[] { name };
	}

	public boolean canHandle(Session session, String text) {
		return JMShellUtil.isCommand(text, command)
				|| JMShellUtil.isCommand(text, alias);
	}

	public abstract String getHelp(Session session);
}
